package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.model.Area;
import com.lonty.intelligence.service.AreaService;
import com.lonty.intelligence.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/4/17 11:52
 * @Version 1.0
 */
@RestController
@RequestMapping("app/area")
@Validated
@Api(tags = "小区接口", produces = "application/json")
public class AreaController {

    @Resource
    private AreaService areaService;

    @ApiOperation(value = "保存小区", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名字",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("saveArea")
    public Result saveArea(String communityId, String name){
        Area area = new Area();
        area.setId(UUIDUtil.randomID());
        area.setCommunityId(communityId);
        area.setName(name);
        area.setIsDel(0);
        if(areaService.saveArea(area)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "根据社区ID获取小区", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId", value = "社区ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("listArea")
    public Result listArea(String communityId){
        return Result.builder()
                .data(areaService.listArea(communityId))
                .build();
    }

}
